<?php
/* SVN FILE: $Id: users_controller.php 20 2009-05-02 13:50:00Z ake.svedin $ */
/**
 * Travian Alliance Coordinator
 * Copyright (c) 2009 Åke Svedin.
 *
 * This file is part of Travian Alliance Coordinator.
 *
 * Travian Alliance Coordinator is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Travian Alliance Coordinator is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Travian Alliance Coordinator.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author $Author: ake.svedin $
 * @copyright Copyright (c) 2009 Åke Svedin.
 * @version $Revision: 20 $
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3
 */

App::import('Sanitize');

class UsersController extends AppController {

    var $name = 'Users';
    var $helpers = array('Html', 'Form', 'Travian');
    var $components = array('RequestHandler');

    function login() {
        if (!$this->Auth->user()) {  
            return;  
        }  
   
        if (empty($this->data)) {  
            $this->redirect($this->Auth->redirect());  
        }  
   
        if (empty($this->data['User']['remember_me'])) {  
            $this->RememberMe->delete();  
        }
        else {  
            $this->RememberMe->remember (  
                $this->data['User']['username'],
                $this->data['User']['password']
            );  
        }  
   
        unset($this->data['User']['remember_me']);  
        $this->redirect($this->Auth->redirect());
    }
    function logout() {
        $this->Session->del('Permissions');
        $this->RememberMe->delete();
        $this->redirect($this->Auth->logout());
    }

    function index() {
        $this->set('Menu_active', 'start');
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    // View any users profile, show menu for profile if the logged in user
    // this user.
    function view($id = null) {
        $this->set('Menu_active', 'start');
        if (!$id) {
            $this->Session->setFlash(__('Invalid User.', true));
            $this->redirect(array('action'=>'index'));
        }
        if($id == $this->Auth->user('id')) {
            $this->set('Menu_active', 'profile');
        }
        $this->set('user', $this->User->read(null, $id));
    }

    // Limited to only edit your profile
    function edit($id = null) {
        $this->set('Menu_active', 'profile');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid User', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            Sanitize::clean($this->data);
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function register() {
        $this->set('Menu_active', 'start');
        if (!empty($this->data)) {
            Sanitize::clean($this->data);
            $this->data['User']['ip'] = $this->RequestHandler->getClientIP();
            $this->data['User']['active'] = 0;
            if($this->User->find('count') == 0) {
                // If this is our first user, make him admin and set as active
                $this->data['User']['active'] = 1;
                $group = $this->User->Group->findByName('Admin');
                $this->data['Group'] = array('Group' => $group['Group']['id']);
            }

            $this->User->create();

            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        }
    }

    function admin_index() {
        $this->set('Menu_active', 'admin');
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
    }

    function admin_view($id = null) {
        $this->set('Menu_active', 'admin');
        if (!$id) {
            $this->Session->setFlash(__('Invalid User.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    function admin_add() {
        $this->set('Menu_active', 'admin');
        if (!empty($this->data)) {
            Sanitize::clean($this->data);
            $this->data['User']['ip'] = $this->RequestHandler->getClientIP();
            $this->data['User']['active'] = 0;
            if($this->User->find('count') == 0) {
                // If this is our first user, make him admin and set as active
                $this->data['User']['active'] = 1;
                $group = $this->User->Group->findByName('Admin');
                $this->data['Group'] = array('Group' => $group['Group']['id']);
            }

            $this->User->create();

            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        }
    }

    function admin_edit($id = null) {
        $this->set('Menu_active', 'admin');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid User', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            Sanitize::clean($this->data);
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $groups = $this->User->Group->find('list');
        $this->set(compact('groups'));
    }

    function admin_delete($id = null) {
        $this->set('Menu_active', 'admin');
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for User', true));
            $this->redirect(array('action'=>'index'));
        }
        if ($this->User->del($id)) {
            $this->Session->setFlash(__('User deleted', true));
            $this->redirect(array('action'=>'index'));
        }
    }

}
?>